package com.ruoyi.web.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.page.PageUtils;
import com.ruoyi.common.utils.page.Query;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.web.domain.UcQuoProject;
import com.ruoyi.web.domain.UcQuoProjectItem;
import com.ruoyi.web.domain.UcQuoTable;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.mapper.UcQuoProjectItemMapper;
import com.ruoyi.web.mapper.UcQuoProjectMapper;
import com.ruoyi.web.mapper.UcQuoTableMapper;
import com.ruoyi.web.service.IUcQuoTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class UcQuoTableServiceImpl extends ServiceImpl<UcQuoTableMapper, UcQuoTable> implements IUcQuoTableService {

    @Autowired
    private UcQuoProjectMapper ucQuoProjectMapper;

    @Autowired
    private UcQuoProjectItemMapper ucQuoProjectItemMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public PageUtils listByPage(QueryVo queryVo) {

        QueryWrapper<UcQuoTable> qw =new QueryWrapper<>();

        if(queryVo.getQIsUsed()!=null){
            qw.eq("is_used",queryVo.getQIsUsed());
        }
        if(!StringUtils.isEmpty(queryVo.getQKey())){
            qw.and((wrapper)->{
                wrapper.or().like("name",queryVo.getQKey());
            });
        }

        if("descending".equals(queryVo.getIsAsc())){
            qw.orderBy(true,false,queryVo.getOrderByColumn()).orderBy(true,false,"id");
        }else if("ascending".equals(queryVo.getIsAsc())){
            qw.orderBy(true,true,queryVo.getOrderByColumn()).orderBy(true,false,"id");
        }else{
            qw.orderBy(true,false,"id");
        }

        IPage<UcQuoTable> page = this.page(new Query<UcQuoTable>().getPage(BeanUtil.beanToMap(queryVo)), qw);

        return new PageUtils(page);
    }

    @Override
    public int insert(UcQuoTable ucQuoTable) {

        ucQuoTable.setIsUsed(0);
        ucQuoTable.setDelStatus(1);
        baseMapper.insert(ucQuoTable);


        for (UcQuoProject ucQuoProject : ucQuoTable.getUcQuoProjects()) {
            ucQuoProject.setId(null);
            ucQuoProject.setTableId(ucQuoTable.getId());
            ucQuoProject.setDelStatus(1);
            ucQuoProjectMapper.insert(ucQuoProject);

            for (UcQuoProjectItem ucQuoProjectItem : ucQuoProject.getUcQuoProjectItems()) {
                ucQuoProjectItem.setOptionId(ucQuoProjectItem.getId());
                ucQuoProjectItem.setId(null);
                ucQuoProjectItem.setTableId(ucQuoTable.getId());
                ucQuoProjectItem.setProjectId(ucQuoProject.getId());
                ucQuoProjectItem.setQuotaType(ucQuoProject.getQuotaType());
                if(ucQuoProjectItem.getScore()==null){
                    ucQuoProjectItem.setScore(0D);
                }
                ucQuoProjectItem.setDelStatus(1);
                ucQuoProjectItemMapper.insert(ucQuoProjectItem);
            }
        }
        return 1;
    }

    @Override
    public int update(UcQuoTable ucQuoTable) {

        QueryVo queryVo = new QueryVo();
        queryVo.setQId(ucQuoTable.getId());
        UcQuoTable oldUcQuoTable = baseMapper.searchBayId(queryVo);

        List<UcQuoProject> addUcQuoProjects = BeanUtils.compareDifference(
                ucQuoTable.getUcQuoProjects(),
                UcQuoProject::getQuotaId,
                oldUcQuoTable.getUcQuoProjectList(),
                UcQuoProject::getQuotaId
        );

        List<UcQuoProject> delUcQuoProjects = BeanUtils.compareDifference(
                oldUcQuoTable.getUcQuoProjectList(),
                UcQuoProject::getQuotaId,
                ucQuoTable.getUcQuoProjects(),
                UcQuoProject::getQuotaId
        );

        List<UcQuoProject> updateUcQuoProjects = BeanUtils.compareSame(
                ucQuoTable.getUcQuoProjects(),
                UcQuoProject::getQuotaId,
                oldUcQuoTable.getUcQuoProjectList(),
                UcQuoProject::getQuotaId
        );

        for (UcQuoProject ucQuoProject : addUcQuoProjects) {
            ucQuoProject.setId(null);
            ucQuoProject.setTableId(ucQuoTable.getId());
            ucQuoProject.setDelStatus(1);
            ucQuoProjectMapper.insert(ucQuoProject);

            for (UcQuoProjectItem ucQuoProjectItem : ucQuoProject.getUcQuoProjectItems()) {
                ucQuoProjectItem.setOptionId(ucQuoProjectItem.getId());
                ucQuoProjectItem.setId(null);
                ucQuoProjectItem.setTableId(ucQuoTable.getId());
                ucQuoProjectItem.setProjectId(ucQuoProject.getId());
                ucQuoProjectItem.setQuotaType(ucQuoProject.getQuotaType());
                if(ucQuoProjectItem.getScore()==null){
                    ucQuoProjectItem.setScore(0D);
                }
                ucQuoProjectItem.setDelStatus(1);
                ucQuoProjectItemMapper.insert(ucQuoProjectItem);
            }
        }


        for (UcQuoProject ucQuoProject : updateUcQuoProjects) {

            UcQuoProject oldUcQuoProject = oldUcQuoTable.getUcQuoProjectList().stream().filter(item -> {
                return item.getQuotaId().equals(ucQuoProject.getQuotaId());
            }).findAny().get();

            oldUcQuoProject.setIndexNo(ucQuoProject.getIndexNo());
            for (UcQuoProjectItem ucQuoProjectItem : ucQuoProject.getUcQuoProjectItems()) {
                UpdateWrapper<UcQuoProjectItem> uwUcQuoProjectItem = new UpdateWrapper<>();
                uwUcQuoProjectItem.eq("table_id",ucQuoTable.getId());
                uwUcQuoProjectItem.eq("project_id",oldUcQuoProject.getId());
                if(ucQuoProject.getChooseType().equals(2) || ucQuoProject.getChooseType().equals(3)){
                    uwUcQuoProjectItem.eq("option_id",ucQuoProjectItem.getOptionId());
                }

                if(ucQuoProjectItem.getScore() == null){
                    ucQuoProjectItem.setScore(0D);
                }
                uwUcQuoProjectItem.set("score",ucQuoProjectItem.getScore());
                uwUcQuoProjectItem.set("quota_from",ucQuoProjectItem.getQuotaFrom());
                uwUcQuoProjectItem.set("limit_type",ucQuoProjectItem.getLimitType());
                uwUcQuoProjectItem.set("limit_max",ucQuoProjectItem.getLimitMax());
                ucQuoProjectItemMapper.update(null,uwUcQuoProjectItem);
            }

            ucQuoProjectMapper.updateById(ucQuoProject);
        }

        List<Long> ids = delUcQuoProjects.stream().map(UcQuoProject::getId).collect(Collectors.toList());
        if(!StringUtils.isEmpty(ids)){
            ucQuoProjectMapper.deleteBatchIds(ids);
        }

        return 1;
    }



    @Override
    public List<UcQuoProject> getUcQuoProject(Long id) {

        QueryWrapper<UcQuoProject> qwUcQuoProject = new QueryWrapper<>();
        qwUcQuoProject.eq("table_id",id);
        qwUcQuoProject.orderByAsc("index_no");
        List<UcQuoProject> ucQuoProjects = ucQuoProjectMapper.selectList(qwUcQuoProject);

        QueryWrapper<UcQuoProjectItem> qwUcQuoProjectItem = new QueryWrapper<>();
        qwUcQuoProjectItem.eq("table_id",id);
        List<UcQuoProjectItem> ucQuoProjectItems = ucQuoProjectItemMapper.selectList(qwUcQuoProjectItem);

        ucQuoProjects.forEach(ucQuoProject -> {

            List<UcQuoProjectItem> currentList = ucQuoProjectItems.stream().filter(ucQuoProjectItem -> {
                return ucQuoProjectItem.getProjectId().equals(ucQuoProject.getId());
            }).collect(Collectors.toList());

            ucQuoProject.setUcQuoProjectItems(currentList);
        });

        return ucQuoProjects;
    }

    @Override
    public void setIsUsed(UcQuoTable ucQuoTable) {
        UpdateWrapper<UcQuoTable> uwUcQuoTable = new UpdateWrapper<>();
        uwUcQuoTable.eq("is_used",1);
        uwUcQuoTable.set("is_used",0);
        baseMapper.update(null,uwUcQuoTable);

        ucQuoTable.setIsUsed(1);
        baseMapper.updateById(ucQuoTable);

    }

}
